/*
 * @lc app=leetcode.cn id=572 lang=cpp
 *
 * [572] 另一个树的子树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
/**
 * @brief 判断是否为子树，及是否相等，或者是否与左子树、右子树相等
 * 
 * @param s 
 * @param t 
 * @return true 
 * @return false 
 */
    bool isSubtree(TreeNode* s, TreeNode* t) {
        if(s == NULL && t == NULL)
            return true;
        if(s == NULL && t != NULL)
            return false;
        return isSame(s,t) || isSubtree(s->left,t) ||isSubtree(s->right,t);
    }

    bool isSame(TreeNode* s, TreeNode* t)
    {
        if(s == NULL && t == NULL)
            return true;
        return s && t 
                && s->val == t->val
                && isSame(s->left,t->left)
                && isSame(s->right,t->right);
    }
};
// @lc code=end

